import 'package:flutter/material.dart';

void main() => runApp(const MyApp());

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  static const String _title = 'Flutter Code Sample';

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: _title,
      home: Scaffold(
        appBar: AppBar(title: const Text(_title)),
        body: const MyStatefulWidget(),
      ),
    );
  }
}

class MyStatefulWidget extends StatefulWidget {
  const MyStatefulWidget({super.key});

  @override
  State<MyStatefulWidget> createState() => _MyStatefulWidgetState();
}

class _MyStatefulWidgetState extends State<MyStatefulWidget> with SingleTickerProviderStateMixin{

  late AnimationController _animationController;

  @override
  void initState() {
    super.initState();
    // 动画控制器
    _animationController = AnimationController(
        vsync: this,
        duration: Duration(milliseconds: 1400)
    )..addListener(() {
      setState(() {

      });
    });
  }

  double sliderValue = 0;
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Color.fromARGB(255, 255, 115, 0),
      child: Theme(
        data: Theme.of(context).copyWith(
          sliderTheme: SliderThemeData(
            thumbShape: SquareSliderComponentShape(),
            trackShape: MyRoundedRectSliderTrackShape(),
          ),
        ),
        child: Slider(
          onChanged: (value) => setState(() => sliderValue = value),
          value: sliderValue,
          min: 0,
          max: 100,
          activeColor: Colors.red,
          inactiveColor: const Color.fromARGB(255, 230, 209, 138),
        ),
      ),
    );
    // Slider(
    //   value: _currentSliderValue,
    //   max: 100,
    //   divisions: 5,
    //   label: _currentSliderValue.round().toString(),
    //   onChanged: (double value) {
    //     setState(() {
    //       _currentSliderValue = value;
    //     });
    //   },
    // );
  }

}

class SquareSliderComponentShape extends SliderComponentShape {
  @override
  Size getPreferredSize(bool isEnabled, bool isDiscrete) {
    return const Size(30, 30);
  }

  @override
  void paint(PaintingContext context, Offset center,
      {required Animation<double> activationAnimation,
        required Animation<double> enableAnimation,
        required bool isDiscrete,
        required TextPainter labelPainter,
        required RenderBox parentBox,
        required SliderThemeData sliderTheme,
        required TextDirection textDirection,
        required double value,
        required double textScaleFactor,
        required Size sizeWithOverflow}) {

    final Canvas canvas = context.canvas;
    // canvas.drawShadow(
    //     Path()
    //       ..addRRect(RRect.fromRectAndRadius(
    //         Rect.fromCenter(center: center, width: 24, height: 30),
    //         const Radius.circular(4),
    //       )),
    //     Colors.black,
    //     5,
    //     false);
    canvas.drawRRect(
        RRect.fromRectAndRadius(
          Rect.fromCenter(center: center, width: 30, height: 30),
          const Radius.circular(15),
        ),
        Paint()..color = HSVColor.fromAHSV(1, 0, 1, 1).toColor()
    );
    canvas.drawCircle(center, 8,Paint()..color = HSVColor.fromAHSV(1, 30, 1, 1).toColor());
  }
}

class MyRoundedRectSliderTrackShape extends SliderTrackShape with BaseSliderTrackShape {
  const MyRoundedRectSliderTrackShape();

  @override
  void paint(PaintingContext context, Offset offset, {
    required RenderBox parentBox,
    required SliderThemeData sliderTheme,
    required Animation<double> enableAnimation,
    required TextDirection textDirection,
    required Offset thumbCenter,
    bool isDiscrete = false,
    bool isEnabled = false,
    double additionalTrackHeight = 10,
  }) {

    if (sliderTheme.trackHeight == null || sliderTheme.trackHeight! <= 0) {
      return;
    }

    Color red1 = HSVColor.fromAHSV(1, 0, 1, 0).toColor();
    Color red2 = HSVColor.fromAHSV(1, 0, 1, 0.5).toColor();
    Color red3 = HSVColor.fromAHSV(1, 0, 1, 0.6).toColor();
    Color red4 = HSVColor.fromAHSV(1, 0, 1, 0.7).toColor();
    Color red5 = HSVColor.fromAHSV(1, 0, 1, 0.8).toColor();
    Color red6 = HSVColor.fromAHSV(1, 0, 1, 0.9).toColor();
    Color red7 = HSVColor.fromAHSV(1, 0, 1, 1).toColor();
    LinearGradient linear = LinearGradient(
      begin: Alignment.centerLeft,
      end: Alignment.centerRight,
      colors: <Color>[red1,red2,red3,red4,red5,red6,red7],
    );

    final ColorTween activeTrackColorTween = ColorTween(
        begin: sliderTheme.disabledActiveTrackColor,
        end: sliderTheme.activeTrackColor);
    final ColorTween inactiveTrackColorTween = ColorTween(
        begin: sliderTheme.disabledInactiveTrackColor,
        end: sliderTheme.inactiveTrackColor);

    final Paint activePaint = Paint()
      ..color = activeTrackColorTween.evaluate(enableAnimation)!;
    final Paint inactivePaint = Paint()
      ..color = inactiveTrackColorTween.evaluate(enableAnimation)!;

    final Paint leftTrackPaint;
    final Paint rightTrackPaint;

    switch (textDirection) {
      case TextDirection.ltr:
        leftTrackPaint = activePaint;
        rightTrackPaint = inactivePaint;
        break;
      case TextDirection.rtl:
        leftTrackPaint = inactivePaint;
        rightTrackPaint = activePaint;
        break;
    }

    final Rect trackRect = getPreferredRect(
      parentBox: parentBox,
      offset: offset,
      sliderTheme: sliderTheme,
      isEnabled: isEnabled,
      isDiscrete: isDiscrete,
    );

    final Radius activeTrackRadius =
    Radius.circular((trackRect.height + additionalTrackHeight) / 2);

    context.canvas.drawRRect(
      RRect.fromLTRBAndCorners(
        trackRect.left,
        trackRect.top - (additionalTrackHeight / 2),
        thumbCenter.dx,
        trackRect.bottom + (additionalTrackHeight / 2),
        topLeft: activeTrackRadius,
        bottomLeft: activeTrackRadius,
      ),
      leftTrackPaint..shader = linear.createShader(Offset(trackRect.left,0) & Size(trackRect.right - trackRect.left, trackRect.bottom)),
    );
    context.canvas.drawRRect(
      RRect.fromLTRBAndCorners(
        thumbCenter.dx,
        trackRect.top - (additionalTrackHeight / 2),
        trackRect.right,
        trackRect.bottom + (additionalTrackHeight / 2),
        topRight: activeTrackRadius,
        bottomRight: activeTrackRadius,
      ),
      rightTrackPaint,
    );
  }
}